﻿#include "information.h"
#include "qsqlquery.h"
#include "ui_information.h"
#include "global.h"
#include "change_info.h"
#include"QPixmap"
#include"QPainter"
#include"QPainterPath"
#include"QFileDialog"
Information::Information(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Information)
{
    ui->setupUi(this);
//    bok =new book_sql;
//    Readersql* readersql = global::read;
    a=nullptr;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("C:\\LIBDB\\data.db");
    user = global::user;
    QSqlQuery query(db);
    if(db.open()){

    }else{
        qDebug()<<"not open !"<<Qt::endl;
    }
//    QSqlQuery query(readersql->m_db);
    QString command = "SELECT * FROM Reader WHERE ID = "+user;
    query.exec(command);
    if(query.next()){
//        qDebug()<<query.value("email").toString();
        ui->mail->setText(query.value("email").toString());
        ui->mail->adjustSize();
    }else{
        qDebug()<<"none"<<Qt::endl;
    }

    ui->name->setText(user);
    ui->name->adjustSize();
    QSqlTableModel *model;
//    model = new QSqlTableModel(this,bok->book_db);//设置表
    model = new QSqlTableModel(this,db);
    model->setTable("book_info");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);

    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();
    ui->tableView->setAlternatingRowColors(true);

//    QSqlQuery sql(bok->book_db);
    QSqlQuery sql(db);
    QString strSql = QString("SELECT * FROM book_info WHERE borrower_ID =%1").arg(user);
    sql.exec(strSql);
    QString sname = QString("borrower_ID = '%1'").arg(user);//打包
    model->setFilter(sname);
    model->select();      //再次显示一次

    while(sql.next())
    {
        qDebug()<<"Found"<<sql.value(0).toString()<<sql.value(1).toString()<<sql.value(2).toString()<<
            sql.value(3).toString()<<sql.value(4).toString()<<sql.value(5).toString()<<
            sql.value(5).toString()<<sql.value(6).toString()<<sql.value(7).toString();

    }

    //载入图片
    QString path = ":/protrait/th-c1.png";
    QPixmap pixmap(path);
    pixmap = pixmap.scaled(ui->image->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    int width = ui->image->size().width();
    int height = ui->image->size().height();

    QPixmap image(width,height);
    image.fill(Qt::transparent);

    QPainterPath painterPath;
    painterPath.addEllipse(0, 0, width, height);
    QPainter painter(&image);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.setClipPath(painterPath);
    painter.drawPixmap(0, 0, width, height, pixmap);
    //绘制到label
    ui->image->setPixmap(image);

    connect(ui->logoutLabel,&QPushButton::clicked,this,[=](){
        emit logoutClick();
        qDebug()<<"logout signal emit";
    });

}

Information::~Information()
{
    delete a;
    delete ui;
}

void Information::on_pushButton_clicked()
{
    if(a==nullptr){
        a= new Change_info(this);
        connect(a,&Change_info::finished,this,[=](){
            delete a;
            a=nullptr;
            QSqlQuery query(db);
            QString command = "SELECT * FROM Reader WHERE ID = "+user;
            query.exec(command);
            if(query.next()){
        //        qDebug()<<query.value("email").toString();
                ui->mail->clear();
                ui->mail->setText(query.value("email").toString());
                ui->mail->adjustSize();
            }else{
                qDebug()<<"none"<<Qt::endl;
            }
        });
        a->show();
    }else{

    }
//    db = QSqlDatabase::addDatabase("QSQLITE");
//    db.setDatabaseName("D:\\qtproject\\sysuQT\\qt-big-job-master (1)\\qt-big-job-master\\data.db");
//    QSqlQuery query(db);
//    QString command = "SELECT * FROM Reader WHERE ID = "+user;
//    query.exec(command);
//    if(query.next()){
//        ui->mail->clear();
//        ui->mail->setText(query.value("email").toString());
//        ui->mail->adjustSize();
//    }

}


void Information::on_image_change_clicked()
{
    QString curPath=QDir::currentPath();//获取当前路径
    QString str=QFileDialog::getOpenFileName(this,"打开文件",curPath,"JPEG Files(*.jpg);;PNG Files(*.png);;PGM Files(*.pgm)");
    //show图片
    if(str!=""){

        ui->image->clear();
        QPixmap pixmap(str);
        pixmap = pixmap.scaled(ui->image->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

        int width = ui->image->size().width();
        int height = ui->image->size().height();

        QPixmap image(width,height);
        image.fill(Qt::transparent);

        QPainterPath painterPath;
        painterPath.addEllipse(0, 0, width, height);

        QPainter painter(&image);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
        painter.setClipPath(painterPath);
        painter.drawPixmap(0, 0, width, height, pixmap);
        //绘制到label
        ui->image->setPixmap(image);
    }
}

